package com.javasm.security;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.javasm.commons.entity.AxiosResult;
import com.javasm.commons.enums.E;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest req, HttpServletResponse resp, AuthenticationException e) throws IOException, ServletException {
        resp.setContentType("application/json;charset=utf-8");
        String data = StrUtil.format("请求访问：{}，认证失败，无法访问系统资源", req.getRequestURI());
        AxiosResult r=AxiosResult.error(E.NO_LOGIN,data);
        resp.getWriter().print(JSON.toJSONString(r));
    }
}